<?php

namespace app\admin\controller;

use app\admin\model\User;
use think\App;
use think\Controller;

use think\facade\Env;
use think\Image;
use think\Request;


class Base extends Controller
{


//验证登陆
    public function __construct(App $app = null)
    {
        parent::__construct($app);

        if (!session('?user_info')){
            //没有登陆
            $this->redirect('admin/login/index');
        }
        //
        //调用菜单栏检测
        $this->getnav();
        //调用权限检测
        $this->checkauth();





    }
    //左侧菜单栏
    public function getnav(){
        //从session中获取ID
        $role_id =  session('user_info.role_id');
        $id = session('user_info.id');
        $user=User::where('id',$id)->find();
        //   1 为超级管理员
        if ($role_id == 1){

            //超级管理员 直接查表
            $top_nav = \app\admin\model\Auth::where(['pid' => 0 , 'is_nav' => 0])->select();
            //查询二级菜单
            $second_nav = \app\admin\model\Auth::where('pid','>',0)->where('is_nav',0)->select();
        }else{
            //其他人,查询拥有的权限
            $role = \app\admin\model\Role::find($role_id);

            $role_auth_ids = $role->role_auth_ids;
            //在查询权限表
            $top_nav = \app\admin\model\Auth::whereIn('id',$role_auth_ids)->where('pid',0)->where('is_nav',0)->select();

            //二级权限
            $second_nav = \app\admin\model\Auth::whereIn('id',$role_auth_ids)->where('pid','>',0)->where('is_nav',0)->select();

        }

        //模板赋值

        $this->assign('top_nav',$top_nav);
        $this->assign('second_nav',$second_nav);
        $this->assign('u',$user);



    }

    //权限检测
    public function checkauth(){

        //从session获取用户ID
        $role_id = session('user_info.role_id');
        //判断是否为超级管理员
        if ($role_id == 1 ) {
            return;
        }
        //获取控制器
        $controller = \request()->controller();
        //获取方法
        $action = \request()->Action();

        if ($controller == 'Homepage' && $action == 'index' || $controller == 'Homepage' && $action =='welcome' ||$controller == 'Base' && $action =='upload'){
            return;
        }
        //权限检测
        $auth = \app\admin\model\Auth::where(['auth_c' =>$controller,'auth_a' => $action])->find();
        $auth_id = $auth['id'];

        //查询角色拥有的权限
        $role= \app\admin\model\Role::find($role_id);
        //字符串变数组
        $role_auth_ids = explode(',' ,$role['role_auth_ids']);
        //判读
        if (!in_array($auth_id,$role_auth_ids)){

         $this->error('权限不足,需要此功能，请联系管理员');


        }


    }










    /*
     * 图片上传接口
     * 等待修改为可以多个上穿
     *
     * */
    public function upload(Request $request)
    {
        $file = request()->file('file');

        if ($file) {
            $info = $file->move('./uploads');

            if ($info) {
                //原图
                 $img='/uploads/' . str_replace('\\', '/', $info->getSaveName());
                //生成缩略图

               $temp = explode(DS,$info->getSaveName());
            $url = DS .'uploads' .DS . $temp[0] . DS . 'thumb_150_'.$temp[1];

                 $image = Image::open('.'.$img)->thumb(150, 150,\think\Image::THUMB_CENTER)
                     ->save('.'.$url);


                $result = [
                    'code'     => 1,
                    'msg'      => '上传成功',
                    'filename' => $img,
                    'thumb'=>$url
                ];
                return json_encode($result);
            } else {
                return false;
            }

        }

    }
}